package top.zhimingw.controller;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.misc.Cache;
import top.zhimingw.pojo.Article;
import top.zhimingw.redis.ArticleKey;
import top.zhimingw.utils.CacheUtils;
import top.zhimingw.utils.LogUtils;

/**
 * @ClassName: ArticleController
 * @Description:
 * @Author:zhimingw
 * @Version 1.0
 * @since 2021/7/14 16:25
 */
@Controller
public class ArticleController extends BaseController{
    private Logger logger = LogUtils.getInstance(AdminController.class);

    
    /**
    * @Description: 轻删除【将文章移到回收站】
    * @Param: [id, from]
    * @return: java.lang.String
    * @Author: zhimingw
    * @Date: 2021/7/14
    */
    @GetMapping("/deleteLight")
    public String deleteLight(@RequestParam("id")Integer id, @RequestParam("from")String from){
        Article article = articleService.queryById(id);
        article.setStatus(2);
        if (articleService.update(article)) {
            logger.warn("【成功】将文章移入回收站，ID："+id);
            CacheUtils.cleanArticleCache(redisService,id);
            rabbitTemplate.convertAndSend("es","article.delete", id);
        }else {
            logger.warn("【失败】服务器异常,将文章移入回收站");
        }
        return "redirect:/articles?type="+from;
    }

    /**
    * @Description: 强删除【永久删除】
    * @Param: [id, from]
    * @return: java.lang.String
    * @Author: zhimingw
    * @Date: 2021/7/16
    */
    @GetMapping("/deleteStrong")
    public String deleteStrong(@RequestParam("id") Integer id,@RequestParam("from") String from){
        if(articleService.deleteById(id)){
            logger.warn("【成功】将文章永久删除,ID: "+id);
            CacheUtils.cleanArticleCache(redisService,id);
            commentService.deleteByArticle(id);
        }else{
            logger.warn("【失败】服务器异常，将文章永久删除，ID："+id);
        }
        return "redirect:/articles?type="+from;
    }

    /**
    * @Description: 还原文章（放入草稿箱)
    * @Param: [id, from]
    * @return: java.lang.String
    * @Author: zhimingw
    * @Date: 2021/7/16
    */
    @GetMapping("/restore")
    public String restore(@RequestParam("id")Integer id,@RequestParam("from")String from){
        Article article = articleService.queryById(id);
        article.setStatus(0);
        if(articleService.update(article)){
            logger.warn("【成功】将文章还原到草稿箱");
            CacheUtils.cleanArticleCache(redisService,id);
            rabbitTemplate.convertAndSend("es","article.delete",id);
        }else{
            logger.warn("【失败】将文章还原到草稿箱");
        }
        return "redirect:/articles?type="+from;
    }
    
    /**
    * @Description: 清空回收站
    * @Param: []
    * @return: java.lang.String
    * @Author: zhimingw
    * @Date: 2021/7/16
    */
    @GetMapping("/cleanTrash")
    public String cleanTrash(){
        if(articleService.deleteTrash()>0){
            logger.warn("【成功】清空回收站");
            CacheUtils.cleanArticleCache(redisService,null);
        }else{
            logger.warn("【失败】清空回收站");
        }
        return "redirect:/articles";
    }

    /**
    * @Description: 更新文章
    * @Param: [id, title, content, tags, kind, introduce, status]
    * @return: java.lang.String
    * @Author: zhimingw
    * @Date: 2021/7/21
    */
    @PostMapping("/update")
    @ResponseBody
    public String update(Integer id,String title,String content,String tags,String kind,String introduce,Integer status){
        if(articleService.update(id,title,content,tags,kind,introduce,status)){
            result.setCode(200);
            logger.warn("【成功】文章更新成功，ID："+id);
            result.setData(id);
            CacheUtils.cleanArticleCache(redisService,id);
            if(status==1){
                rabbitTemplate.convertAndSend("es","article.save",id);
            }
        }else{
            logger.warn("【失败】文章更新，没有修改任何信息");
        }
        return JSONObject.toJSONString(result);
    }

    /**
    * @Description: 发布文章
    * @Param: [title, content, tags, kind, introduce]
    * @return: java.lang.String
    * @Author: zhimingw
    * @Date: 2021/7/22
    */
    @PostMapping("/publish")
    @ResponseBody
    public String publish(String title,String content,String tags,String kind,String introduce){
        Integer id = articleService.publish(title, content, tags, kind, introduce, 1);
        if(id!=null&&id!=0){
            result.setCode(200);
            logger.warn("【成功】文章发布成功，ID："+id);
            result.setData(id);
            CacheUtils.cleanArticleCache(redisService,null);
            rabbitTemplate.convertAndSend("es","article.save",id);
            redisService.incr(ArticleKey.getEsCount,"");
        }else{
            result.setCode(500);
            logger.warn("【失败】文章发布失败，ID："+id);
        }
        return JSONObject.toJSONString(result);
    }

    /**
    * @Description: 保存草稿
    * @Param: [title, content, tags, kind, introduce]
    * @return: java.lang.String
    * @Author: zhimingw
    * @Date: 2021/7/22
    */
    @PostMapping("/draftPublish")
    @ResponseBody
    public String draftPublish(String title,String content,String tags,String kind,String introduce){
        Integer id = articleService.publish(title, content, tags, kind,introduce,0);
        if(id!=null&&id!=0){
            result.setCode(200);
            logger.warn("【成功】文章草稿保存成功，ID："+id);
            result.setData(id);
        }else{
            result.setCode(500);
            logger.warn("【失败】文章发布失败，ID："+id);
        }
        return JSONObject.toJSONString(result);
    }
}   
